package com.rzcoding.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rzcoding.dao.GoodsMapper;
import com.rzcoding.dao.GoodsPicsMapper;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.dto.req.GoodsReqMo;
import com.rzcoding.entity.Goods;
import com.rzcoding.entity.GoodsPics;
import com.rzcoding.exception.BussinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsPicsMapper goodsPicsMapper;

    public PageInfo<Goods> selectByUsername(String userName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Goods goods = new Goods();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(userName)){
            goods.setGoodsName(userName+"%");
        }
        Example example = createExample(goods);
        example.setOrderByClause("UPD_TIME DESC");
        return new PageInfo<>(goodsMapper.selectByExample(example));
    }

    public void delete(Integer id){
        goodsMapper.deleteByPrimaryKey(id);
    }


    @Transactional
    public void add(GoodsReqMo goodsReqMo){
        String goodsCat = goodsReqMo.getGoodsCat();
        if(StringUtils.isEmpty(goodsCat)){
            throw new BussinessException(HttpStatus.BAD_REQUEST.value(), ResultConst.INVALID_GOODS_CAT);
        }
        String[] catIds = goodsCat.split(",");
        if(catIds.length != 3){
            throw new BussinessException(HttpStatus.BAD_REQUEST.value(), ResultConst.INVALID_GOODS_CAT);
        }
        Goods goods = new Goods();
        goods.setGoodsName(goodsReqMo.goodsName);
        goods.setGoodsNumber(goodsReqMo.goodsNumber);
        goods.setGoodsWeight(goodsReqMo.goodsWeight);
        goods.setGoodsPrice(goodsReqMo.getGoodsPrice());
        goods.setCatOneId(Integer.parseInt(catIds[0]));
        goods.setCatTwoId(Integer.parseInt(catIds[1]));
        goods.setCatThreeId(Integer.parseInt(catIds[2]));
        goods.setGoodsIntroduce(goodsReqMo.goodsIntroduce);

        Long time = new Date().getTime() / 1000;
        goods.setAddTime(time.intValue());
        goods.setUpdTime(time.intValue());
        goodsMapper.insertSelective(goods);
        Integer goodsId = goods.getId();

        List<String> pics = goodsReqMo.pics;
        if(CollectionUtils.isNotEmpty(pics)){
            for (String pic : pics) {
                GoodsPics goodsPics = new GoodsPics();
                goodsPics.setPics(pic);
                goodsPics.setGoodsId(goodsId);
                goodsPicsMapper.insert(goodsPics);
            }
        }
    }

    /**
     * Goods构建查询对象
     * @param goods
     * @return
     */
    public Example createExample(Goods goods){
        Example example=new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        if(goods!=null){
            // 主键id
            if(!StringUtils.isEmpty(goods.getId())){
                criteria.andEqualTo("id",goods.getId());
            }
            // 商品名称
            if(!StringUtils.isEmpty(goods.getGoodsName())){
                criteria.andLike("goodsName",goods.getGoodsName());
            }
            // 商品价格
            if(!StringUtils.isEmpty(goods.getGoodsPrice())){
                criteria.andEqualTo("goodsPrice",goods.getGoodsPrice());
            }
            // 商品数量
            if(!StringUtils.isEmpty(goods.getGoodsNumber())){
                criteria.andEqualTo("goodsNumber",goods.getGoodsNumber());
            }
            // 商品重量
            if(!StringUtils.isEmpty(goods.getGoodsWeight())){
                criteria.andEqualTo("goodsWeight",goods.getGoodsWeight());
            }
            // 商品详情介绍
            if(!StringUtils.isEmpty(goods.getGoodsIntroduce())){
                criteria.andEqualTo("goodsIntroduce",goods.getGoodsIntroduce());
            }
            // 添加商品时间
            if(!StringUtils.isEmpty(goods.getAddTime())){
                criteria.andEqualTo("addTime",goods.getAddTime());
            }
            // 修改商品时间
            if(!StringUtils.isEmpty(goods.getUpdTime())){
                criteria.andEqualTo("updTime",goods.getUpdTime());
            }
            // 一级分类id
            if(!StringUtils.isEmpty(goods.getCatOneId())){
                criteria.andEqualTo("catOneId",goods.getCatOneId());
            }
            // 二级分类id
            if(!StringUtils.isEmpty(goods.getCatTwoId())){
                criteria.andEqualTo("catTwoId",goods.getCatTwoId());
            }
            // 三级分类id
            if(!StringUtils.isEmpty(goods.getCatThreeId())){
                criteria.andEqualTo("catThreeId",goods.getCatThreeId());
            }
        }
        return example;
    }


}
